package com.huluobo.high3demo.login;

import androidx.lifecycle.Observer;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.high3demo.main.MainActivity;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.databinding.ActivityLoginBinding;
import com.huluobo.high3demo.register.RegisterActivity;
import com.huluobo.mvvm.baen.UserBeanResult;
import com.huluobo.mvvm.net.Constant;
import com.huluobo.mvvm.view.BaseMVVMActivity;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

public class LoginActivity extends BaseMVVMActivity<ActivityLoginBinding, LoginViewModel> {

    @Override
    protected void initData() {
        //观察ViewModel中LiveData的值是否发生改变,如果改变,代表成功或者失败已经赋值
        viewModel.loginSuccess.observe(this, new Observer<UserBeanResult.UserBean>() {
            @Override
            public void onChanged(UserBeanResult.UserBean userBean) {
                if (userBean == null) {
                    ToastUtils.showLong("登录失败,用户不存在");
                    return;
                }
                //登录成功保存token
                SPUtils.getInstance().put(Constant.SP_TOKEN, userBean.getToken());
                SPUtils.getInstance().put(Constant.SP_USERNAME, userBean.getUsername());
                //保存一下用户信息
                viewModel.saveUserInfo(userBean);
                //给用户点儿钱
                viewModel.insertUserMoney(userBean.getUsername(), 10000f);
                EMClient.getInstance().login(userBean.getUsername(), userBean.getPassword(), new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        Log.i("TagA", "登录环信成功");
                    }

                    @Override
                    public void onError(int code, String error) {
                        Log.i("TagA", "登录环信失败:" + error);
                    }
                });
                Toast.makeText(LoginActivity.this, "登录成功:" + userBean.getNickname(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        viewModel.loginFailed.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(LoginActivity.this, "登录失败:" + s, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void initView() {
        dataBinding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        dataBinding.tvLoginToRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册页面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

//        Log.i("TagA",
//                "sp:" + SPUtils.getInstance().getString(Constant.SP_TOKEN).isEmpty() + "em:" + EMClient.getInstance().isLoggedInBefore());

//        if (!SPUtils.getInstance().getString(Constant.SP_TOKEN).isEmpty() &&! EMClient.getInstance().isLoggedInBefore()) {
//            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//            startActivity(intent);
//        }
    }

    private void login() {
        String username = dataBinding.edLoginUsername.getText().toString().trim();
        String password = dataBinding.edLoginPassword.getText().toString().trim();
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            ToastUtils.showLong("请输入用户名和密码");
            return;
        }
        //验证合法
//        viewModel.login(username, password);
        viewModel.loginJson(username, password);
    }


    // 相当于new Presenter(),可以通过presenter来触发login方法
    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
}